<?php
// +----------------------------
// | Created by PhpStorm
// +----------------------------
// | Author: YnnSon
// +----------------------------
namespace app\api\model;

use think\Db;

class Subscribe extends BaseModel
{
    /**
     * @Desc 获取预约时间段及剩余数量列表
     *
     * @param $param
     *
     * @return array
     */
    public static function getList($param)
    {
        $date_id = $param['date_id'];
        $company_id = $param['company_id'];
        $vehicle_id = $param['vehicle_id'];
        // date_id=131&company_id=45&vehicle_id=2
        $date_date = Db::name('date')->where('id', '=', $date_id)->value('date_date');
        $time_state = Db::name('time_state')->where('status', '=', 1)->order('time_state')->select();
        $sixHour = mktime(6, 0, 0, date('m'), date('d'), date('Y')); // 当天6点开始预约
        $today = date('Y-m-d'); // 今天的时间
        $oneHoursAgo = date('H', strtotime('-1 hours')); // 一小时前的小时数
        $num = Db::name('company')->where('id', '=', $company_id)->value('subscribe_num');
        $num_gas = $num_diesel = 0;
        if (45 == $company_id) {
            $num_gas = Db::name('company')->where('id', '=', $company_id)->value('subscribe_num_gas');
            $num_diesel = Db::name('company')->where('id', '=', $company_id)->value('subscribe_num_diesel');
        }
        $list = [];
        foreach ($time_state as $key => $value) {
            $subscribe = (new self)->where('date_id', '=', $date_id)->where('company_id', '=', $company_id)->where('time_state_id', '=', $value['id'])->find();
            if (!$subscribe) {
                $updateData = [
                    'date_id'       => $date_id,
                    'company_id'    => $company_id,
                    'time_state_id' => $value['id'],
                    'num'           => $num,
                    'num_gas'       => $num_gas,
                    'num_diesel'    => $num_diesel,
                ];
                $subscribe = self::create($updateData);
            }
            $item['id'] = $subscribe['id'];
            $item['time_state_name'] = date('H:i', strtotime($value['time_state']));
            $item['date'] = $date_date;
            $thisHours = date('H', strtotime($value['time_state'])); // 本次循环的小时数
            $thistime = time(); // 本次循环的小时数
            // 时间是今天并且一小时前的时间段禁止预约
            if ($thistime < $sixHour || ($date_date == $today && $oneHoursAgo >= $thisHours)) {
                $item['num'] = 0;
            } else {
                $item['num'] = $subscribe['num'];
                if (45 == $company_id && 1 == $vehicle_id) {
                    $item['num'] = $subscribe['num_gas'];
                } elseif (45 == $company_id && (2 == $vehicle_id || 3 == $vehicle_id)) {
                    $item['num'] = $subscribe['num_diesel'];
                }
            }
            $list[] = $item;
        }
        return $list;
    }

    /**
     * @Desc 通过ID获取剩余数量
     * @param $id
     * @param $company_id
     * @param $vehicle_id
     *
     * @return float|mixed|string
     */
    public static function getNumById($id, $company_id, $vehicle_id)
    {
        if (45 == $company_id && 1 == $vehicle_id) {
            $num = (new self)->where('id', '=', $id)->value('num_gas');
        } elseif (45 == $company_id && (2 == $vehicle_id || 3 == $vehicle_id)) {
            $num = (new self)->where('id', '=', $id)->value('num_diesel');
        } else {
            $num = (new self)->where('id', '=', $id)->value('num');
        }
        return $num;
    }

    /**
     * @Desc 取消/未通过订单恢复预约数量
     *
     * @param $id
     *
     * @return Subscribe
     */
    public static function restoreSubscribeNumber($id)
    {
        $subscribe_id = (new Order())->where('order_id', '=', $id)->value('subscribe_id');
        return (new self)->where('id', '=', $subscribe_id)->inc('num')->update();
    }
}